<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model.trim="search_form.configKey" placeholder="配置键" style="width: 200px;" clearable />
      <el-button type="primary" icon="el-icon-search" @click="handle_search">搜索</el-button>
      <el-button @click="handle_reset">重置</el-button>
      <el-button type="success" icon="el-icon-plus" @click="handle_add">新增</el-button>
    </div>

    <el-table
      v-loading="loading"
      :data="config_list"
      style="width: 100%"
      border
    >
      <el-table-column prop="id" label="配置ID" />
      <el-table-column prop="configKey" label="配置键" />
      <el-table-column prop="configValue" label="配置值">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.configValue || ''" placement="top">
            <span>{{ (scope.row.configValue && scope.row.configValue.length > 30) ? scope.row.configValue.substring(0, 30) + '...' : (scope.row.configValue || '') }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="配置描述" />
      <el-table-column prop="groupName" label="分组名称" />
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handle_edit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handle_delete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination
        background
        layout="prev, pager, next, jumper"
        :total="total"
        :page-size="page_size"
        :current-page="page_num"
        @current-change="handle_current_change"
        @size-change="handle_size_change"
      />
    </div>

    <!-- 表单对话框 -->
    <el-dialog
      :title="dialog_title"
      :visible.sync="dialog_visible"
      width="500px"
    >
      <el-form
        :model="config_form"
        ref="config_form"
        label-width="100px"
      >
          <el-form-item label="配置键" prop="configKey">
          <el-input v-model.trim="config_form.config_key" placeholder="请输入配置键" />
        </el-form-item>
        <el-form-item label="配置值" prop="configValue">
          <el-input v-model.trim="config_form.config_value" placeholder="请输入配置值" type="textarea" />
        </el-form-item>
        <el-form-item label="配置描述" prop="description">
          <el-input v-model.trim="config_form.description" placeholder="请输入配置描述" type="textarea" />
        </el-form-item>
        <el-form-item label="分组名称" prop="groupName">
          <el-input v-model.trim="config_form.group_name" placeholder="请输入分组名称" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch v-model="config_form.status" :active-value="1" :inactive-value="0" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_visible = false">取消</el-button>
        <el-button type="primary" @click="handle_submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import sysConfigApi from '@/api/sys-config'

export default {
  name: 'ConfigManagement',
  data() {
    return {
      loading: false,
      config_list: [],
      total: 0,
      page_num: 1,
      page_size: 10,
      search_form: {
        key: ''
      },
      dialog_visible: false,
      dialog_title: '',
      config_form: {
        id: '',
        configKey: '',
        configValue: '',
        description: '',
        groupName: '',
        status: 1
      }
    }
  },
  created() {
    this.get_list()
  },
  methods: {
    // 获取配置列表
    async get_list() {
      this.loading = true
      try {
        const params = {
          page_num: this.page_num,
          page_size: this.page_size,
          ...this.search_form
        }
        const { data } = await sysConfigApi.list(params)
        this.config_list = data.records
        this.total = data.total
      } catch (error) {
        this.$message.error('获取配置列表失败')
      } finally {
        this.loading = false
      }
    },
    
    // 搜索
    handle_search() {
      this.page_num = 1
      this.get_list()
    },
    // 重置搜索条件
    handle_reset() {
      this.search_form = {
        configKey: '',
        group_name: ''
      };
      this.get_list();
    },
    
    // 新增
    handle_add() {
      this.dialog_title = '新增配置'
      this.config_form = {
        id: '',
        configKey: '',
        configValue: '',
        description: '',
        groupName: '',
        status: 1
      }
      this.dialog_visible = true
    },
    
    // 编辑
    handle_edit(row) {
      this.dialog_title = '编辑配置'
      this.config_form = { ...row }
      
      this.dialog_visible = true
    },
    
    // 删除
    async handle_delete(id) {
      this.$confirm('确定要删除此配置吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await sysConfigApi.delete(id)
          this.$message.success('删除成功')
          this.get_list()
        } catch (error) {
          this.$message.error('删除失败')
        }
      })
    },
    
    // 提交表单
    async handle_submit() {
      try {
        if (this.config_form.id) {
             
          await sysConfigApi.update(this.config_form)
          this.$message.success('更新成功')
        } else {
          await sysConfigApi.add(this.config_form)
          this.$message.success('创建成功')
        }
        this.dialog_visible = false
        this.get_list()
      } catch (error) {
        this.$message.error('操作失败')
      }
    },
    
    // 分页处理
    handle_current_change(val) {
      this.page_num = val
      this.get_list()
    },
    
    handle_size_change(val) {
      this.page_size = val
      this.page_num = 1
      this.get_list()
    }
  }
}
</script>

<style scoped>
.filter-container {
  padding: 10px 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>